/* eslint-disable */
import elementHelper from 'bpmn-js-properties-panel/lib/helper/ElementHelper'
import cmdHelper from 'bpmn-js-properties-panel/lib/helper/CmdHelper'
import entryFactory from '../EntryFactory'

import {
  is
} from 'bpmn-js/lib/util/ModelUtil'
import {
  find,
  forEach
} from 'lodash'

const CURRENT_NAME = 'type'
/**
 * Get all camunda:property objects for a specific business object
 *
 * @param  {ModdleElement} parent
 *
 * @return {Array<ModdleElement>} a list of camunda:property objects
 */
function getPropertyValues(parent) {
  var properties = parent && getPropertiesElement(parent);
  if (properties && properties.values) {
    return properties.values;
  }
  return [];
}

/**
 * Get all camunda:Properties object for a specific business object
 *
 * @param  {ModdleElement} parent
 *
 * @return {ModdleElement} a camunda:Properties object
 */
function getPropertiesElement(element) {
  if (!isExtensionElements(element)) {
    return element.properties;
  } else {
    return getPropertiesElementInsideExtensionElements(element);
  }
}
/**
 * Get first camunda:Properties object for a specific bpmn:ExtensionElements
 * business object.
 *
 * @param {ModdleElement} extensionElements
 *
 * @return {ModdleElement} a camunda:Properties object
 */
function getPropertiesElementInsideExtensionElements(extensionElements) {
  return find(extensionElements.values, function (elem) {
    return is(elem, 'camunda:Properties');
  });
}

/**
 * Returns true, if the given business object is a bpmn:ExtensionElements.
 *
 * @param {ModdleElement} element
 *
 * @return {boolean} a boolean value
 */
function isExtensionElements(element) {
  return is(element, 'bpmn:ExtensionElements');
}
export default function TaskTypeProps(group, element, bpmnFactory, translate) {
  if (is(element, 'bpmn:UserTask')) {

    const addElement = function (setValue) {
      if (!element.businessObject.extensionElements) {
        let command = []
        let parent = elementHelper.createElement('bpmn:ExtensionElements', {
          values: []
        }, element.businessObject, bpmnFactory);
        let cmd = cmdHelper.updateBusinessObject(element, element.businessObject, {
          extensionElements: parent
        });
        command.push(cmd)

        let properties = elementHelper.createElement('camunda:Properties', {}, parent, bpmnFactory);

        command.push(cmdHelper.addAndRemoveElementsFromList(
          element,
          parent,
          'values',
          'extensionElements',
          [properties],
          []
        ))

        var propertyProps = {}
        propertyProps.name = CURRENT_NAME
        propertyProps.value = setValue.value

        var propertyC = elementHelper.createElement('camunda:Property', propertyProps, properties, bpmnFactory)
        command.push(cmdHelper.addElementsTolist(element, properties, 'values', [propertyC]));

        return command

      } else {
        let parent = element.businessObject.extensionElements,
          inx = -1
        const propertys = getPropertyValues(parent)

        forEach(propertys, function (property, index) {
          if (property.name === CURRENT_NAME) {
            inx = index
          }
        })

        if (inx === -1) {
          inx = 0
        }
        let property = propertys[inx]

        // 如果有当前的属性节点
        if (property.name === CURRENT_NAME) {
          return cmdHelper.updateBusinessObject(element, property, setValue)
        } else {
          // 如果没有当前的属性节点
          let commandC = []
          let properties = getPropertiesElement(parent)
          // commandC.push(cmdHelper.updateBusinessObject(element, parent, { 'properties': properties }))

          let propertyProps = {}
          propertyProps.name = CURRENT_NAME
          propertyProps.value = setValue.value

          let property = elementHelper.createElement('camunda:Property', propertyProps, properties, bpmnFactory);
          commandC.push(cmdHelper.addElementsTolist(element, properties, 'values', [property]));
          return commandC
        }
      }
    }

    /**
     * 判断有没有当前要设置的属性节点
     */
    function isProperty(element) {
      let parent = element.businessObject.extensionElements,
            inx    = -1
        const propertys = getPropertyValues(parent)

        forEach(propertys, function(property, index) {
          if (property.name === CURRENT_NAME) {
            inx = index
          }
        })

        return inx > -1
    }
    group.entries.push(entryFactory.selectBox({
      id: 'taskType',
      label: '任务类型',
      selectOptions: [
        {
          value: 'examineTask',
          name: '审批任务'
        },
        {
          value: 'formTask',
          name: '填表任务'
        },
      ],
      modelProperty: 'taskType',

      get: function (element) {
        // 注意只有在新创建一个节点的时候才会触发set方法
        const extensionElements = element.businessObject.extensionElements
        if (!extensionElements) {
          document.getElementById('camunda-taskType-ck').click()
        } else if (!isProperty(element)) {
          document.getElementById('camunda-taskType-ck').click()
        }

        let value = 'examineTask'
        if (extensionElements) {
          const valuesArray = extensionElements.values[0].values

          valuesArray.forEach(val => {
            if (val.name === 'type') {
              value = val.value
            }
          })
        }

        return {
          taskType: value
        }
      },

      set: function (element, values) {
        return addElement({
            value: values.taskType
        })
      }
    }))
  }
}